<?php

require_once __DIR__.DS.'blgEntity.php';

abstract class BlgObject extends BlgEntity {
	
	function __construct($guid){
		$this->load($guid);
	}
	
	/**
	 * Loader for this object
	 * @param mixed $data
	 * @param string $type
	 * @throws Exception
	 */
	protected function load($guid){
		$this->initializeAttributes();
		
		if(FALSE!==($attributes=blg_object_get($guid))){
			$this->attributes= array_merge($this->attributes, $attributes);
			return TRUE;
		}

		return FALSE;
	}
	
	/**
	 * (non-PHPdoc)
	 * @see engine/clases/BlgEntity::initializeAttributes()
	 */
	protected function initializeAttributes(){
		parent::initializeAttributes();
		
		$this->attributes['title']= NULL;
		$this->attributes['description']= NULL;		
	}
	
	/**
	 * Reload object from DB
	 */
	protected function reload(){
		if(!empty($this->attributes['guid'])){
			return $this->load($this->attributes['guid']);
		}			
		return FALSE;
	}
	
	public function set_title($title){
		if(blg_object_update_title($this->guid, $title)){
			$this->attributes['title']= $title;
			return TRUE;
		}		
		
		return FALSE;
	}
	
	public function set_description($description){
		if(blg_object_update_description($this->guid, $description)){
			$this->attributes['description']= $description;
			return TRUE;
		}
		
		return FALSE;
	}	
}
